package cn.cena.basic.controller;


import cn.cena.basic.util.AjaxResult;
import cn.cena.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastdfs")
public class FastDfsController {

    @PostMapping
    public AjaxResult upLoad(@RequestPart(required = true,value = "file") MultipartFile file){
        try {
            String originalFilename = file.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
            String path = FastDfsUtil.upload(file.getBytes(), extName);
            return AjaxResult.result().setResultObj(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("系统异常");
        }
    }

    @DeleteMapping
    public AjaxResult del(@RequestParam String path){
        try {
            String pathTmp = path.substring(1);//  group1/xxxxx/xxxx.xxx
            String groupPath = pathTmp.substring(0, pathTmp.indexOf("/"));// group1
            String fileName = pathTmp.substring(pathTmp.indexOf("/") + 1);//  xxxxx/xxxx.xxx
            FastDfsUtil.delete(groupPath, fileName);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("系统异常");
        }
    }
}
